package wei.zhang.algorithm;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;

/**
 * 自定义分表逻辑
 */
public class HintTablesNameAlgorithm implements HintShardingAlgorithm<Long> {

    @Override
    public Collection<String> doSharding(Collection<String> collection, HintShardingValue<Long> hintShardingValue) {

       String key =  hintShardingValue.getLogicTableName() +"_"+hintShardingValue.getValues().toArray()[0];

       if (collection.contains(key)) {
           return Arrays.asList(key);
       }
       throw new UnsupportedOperationException("table route " + key + " 不存在");
    }
}
